package com.lengmou.service.impl;

import com.lengmou.dao.CouponsDao;
import com.lengmou.entity.Coupons;
import com.lengmou.service.CouponsService;
import com.lengmou.util.TimeTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Service("CouponsService")
public class CouponsServiceImpl implements CouponsService {
    @Resource
    private CouponsDao cd;
    @Override
    public List<Coupons> getAll() {
        return cd.querysAll();
    }

    @Override
    public Coupons getCouponsById(String id) {
        return cd.queryById(id);
    }

    @Override
    public Coupons getCouponsByOrdersId(int ordersId) {
        return cd.queryByOrdersid(ordersId);
    }

    @Override
    public List<Coupons> getCouponsByState(int state) {
        return cd.querysByState(state);
    }

    @Override
    public List<Coupons> getCouponsByOrdersIds(List<Integer> orderids) {
        List<Coupons> couponsList = new ArrayList<>();
        for (Integer orderid: orderids) {
            Coupons couponsByOrdersId = getCouponsByOrdersId(orderid);
            if(couponsByOrdersId!=null){
                couponsList.add(couponsByOrdersId);
            }
        }
        return couponsList;
    }

    @Override
    public List<Coupons> getCouponsByCut(double cut) {
        return cd.querysByCut(cut);
    }

    @Override
    public List<Coupons> getCouponsByFull(double full) {
        return cd.querysByFull(full);
    }

    @Override
    public List<Coupons> getCouponsByNotUse() {
        List<Coupons> all = getCouponsByState(0);
        List<Coupons> couponsList = new ArrayList<>();
        for (Coupons c: all) {
            if(c.getOrdersid()==0&&c.getUsetime()==null){
                couponsList.add(c);
            }
        }
        return couponsList;
    }

    @Override
    public List<Coupons> getCouponsByUse() {
        List<Coupons> all = getCouponsByState(1);
        List<Coupons> couponsList = new ArrayList<>();
        for (Coupons c: all) {
            if(c.getOrdersid()!=0&&c.getUsetime()!=null){
                couponsList.add(c);
            }
        }
        return couponsList;
    }
    @Override
    public int addsCoupons(int num, double cut, double full) {
        List<Coupons> couponsList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            couponsList.add(new Coupons(UUID.randomUUID().toString(),cut,full,0,TimeTool.time(),null,0));
        }
        return cd.addsCoupons(couponsList);
    }

    @Override
    public int addCoupons(double cut, double full) {

        return cd.addCoupons(new Coupons(UUID.randomUUID().toString(),cut,full,0,TimeTool.time(),null,0));
    }

    @Override
    public int setCouponsCut(Coupons coupons) {
        Coupons couponsById = getCouponsById(coupons.getId());
        if(couponsById!=null){
            if(coupons.getCut()>0){

                couponsById.setCut(coupons.getCut());
                return cd.updateCoupons(couponsById);
            }
//            满减力度必须大于0
            return -2;
        }
        return -1;
    }

    @Override
    public int setCouponsFull(Coupons coupons) {
        Coupons couponsById = getCouponsById(coupons.getId());
        if(couponsById!=null){
            if(coupons.getFull()>0){
                couponsById.setFull(coupons.getFull());
                return cd.updateCoupons(couponsById);
            }else{
                return -2;
            }

        }
        return -1;
    }

    @Override
    public int setCouponsOrdersId(Coupons coupons) {
        Coupons couponsById = getCouponsById(coupons.getId());
        if(couponsById!=null){
            couponsById.setOrdersid(coupons.getOrdersid());
            return cd.updateCoupons(couponsById);
        }
        return -1;
    }

    @Override
    public int setCouponsUseTime(Coupons coupons) {
        Coupons couponsById = getCouponsById(coupons.getId());
        if(couponsById!=null){
            couponsById.setUsetime(coupons.getUsetime());
            return cd.updateCoupons(couponsById);
        }
        return -1;
    }

    @Override
    public int setCouponsCreateTime(Coupons coupons) {
        Coupons couponsById = getCouponsById(coupons.getId());
        if(couponsById!=null){
            couponsById.setCreatetime(coupons.getCreatetime());
            return cd.updateCoupons(couponsById);
        }
        return -1;
    }

    @Override
    public int setCouponsState(Coupons coupons) {
        Coupons couponsById = getCouponsById(coupons.getId());
        if(couponsById!=null){
            couponsById.setState(coupons.getState());
            return cd.updateCoupons(couponsById);
        }
        return -1;
    }

    @Override
    public int useCoupons(String id, int ordersId) {
        Coupons couponsById = getCouponsById(id);
        if(couponsById!=null){
            couponsById.setUsetime(TimeTool.time());
            couponsById.setOrdersid(ordersId);
            couponsById.setState(1);
            return cd.updateCoupons(couponsById);
        }
        return -1;
    }

    @Override
    public boolean existsUse(double price,String id) {
        Coupons couponsById = getCouponsById(id);
//        判断输入的优惠券是否存在，并且有没有被使用过
        if(couponsById!=null&&couponsById.getState()==0){
//            判断是否满足满减优惠
            if(couponsById.getFull()<=price){
                return true;
            }
        }
        return false;
    }

    @Override
    public int deleteCoupons(String id) {
        Coupons couponsById = getCouponsById(id);
        if (couponsById!=null){
            return cd.deleteCoupons(id);
        }
        return -1;
    }

    @Override
    public List<Coupons> search(Map<String, Object> map) {
        return cd.search(map);
    }
}
